package org.zn.note.jdk.net;

import org.zn.note.common.util.ThreadUtil;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName Client
 * @Author zn
 * @Date 2019/4/4 14:40
 **/
public class BioClient {

    static final String IP = "192.168.4.153";
    static final int PORT = 1016;
    static boolean readflag = false;
    static boolean writeflag = false;

    public static void main(String[] args) throws IOException {
        new BioClient().client();
    }

    private void client() throws IOException {
        Socket socket1 = new Socket(IP, PORT);
        Socket socket2 = new Socket();
        SocketAddress localAddress = new InetSocketAddress("127.0.0.1", 5555);
        SocketAddress remoteAddress = new InetSocketAddress(IP, PORT);
        socket2.bind(localAddress);
        socket2.connect(remoteAddress);
    }

    private void client1() throws IOException {
        Socket socket = new Socket(IP, PORT);
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    while (true) {
                        if (readflag) {
                            String s = reader.readLine();
                            ThreadUtil.printThread(s);
                            readflag = !readflag;
                        }
                        TimeUnit.SECONDS.sleep(1L);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-Read").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    String s = "zn\n";
                    while (true) {
                        if (writeflag) {
                            writer.write(s);
                            writer.flush();
                            writeflag = !writeflag;
                            ThreadUtil.printThread(s);
                            TimeUnit.SECONDS.sleep(1L);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "Thread-Write").start();
    }

    private void systemInClient() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            try {
                String s = reader.readLine();
                switch (s) {
                    case "connect": {
                        ThreadUtil.printThread("connect to " + IP + ":" + PORT);
                        client1();
                        break;
                    }
                    case "in": {
                        readflag = true;
                        break;
                    }
                    case "out": {
                        writeflag = true;
                        break;
                    }
                    default: {
                        ThreadUtil.printThread("no command match");
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
